package com.emep.changzhi.analyse.service.basics;


import com.emep.changzhi.analyse.entity.basics.SysIndustryTypeInfo;
import com.emep.changzhi.analyse.entity.basics.SysMonitorSiteInfo;
import com.emep.changzhi.analyse.entity.basics.SysPollutantParameter;
import com.emep.changzhi.analyse.repository.basics.SysMonitorSiteInfoRepository;
import com.emep.changzhi.analyse.utils.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;


/**
 * 监控站点信息
 */
@Service
public class SysMonitorSiteInfoService {

    @Resource
    private SysMonitorSiteInfoRepository repository;
    private static final Logger logger = LoggerFactory.getLogger(SysEmployeeService.class);

    /**
     *
     * 新增监控站点信息
     *
     * @param sysMonitorSiteInfo
     * @return
     * @throws Exception
     */
    public boolean save( SysMonitorSiteInfo sysMonitorSiteInfo) {
        List<SysMonitorSiteInfo> infos =repository.findAllByMonitorSiteName(sysMonitorSiteInfo.getMonitorSiteName());
        if(infos.isEmpty()){
            repository.save(sysMonitorSiteInfo);
            return true;
        }
        return false;
    }

    /**
     *
     * 根据id 删除监控站点信息
     *
     * @param id
     */
    public boolean delete(String id) {
        boolean result = false;
        try {
            repository.deleteById(id);
            result =true;
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
        }
        return result;
    }
    /**
     *查询所有监控站点信息
     *
     * @return
     */
    public List<SysMonitorSiteInfo> findAll() {
        List<SysMonitorSiteInfo> sysMonitorSiteInfoList = null;
        try {
            sysMonitorSiteInfoList =  repository.findAll();
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
        }
        return sysMonitorSiteInfoList;
    }

    /**
     *
     * 根据id查询监控站点信息
     *
     * @param id
     * @return
     */
    public SysMonitorSiteInfo findById(String id){
        SysMonitorSiteInfo sysMonitorSiteInfo = null;
        try {
            sysMonitorSiteInfo = repository.findSysMonitorSiteInfoById(id);
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
        }
        return sysMonitorSiteInfo;
    }


    /**
     *
     * 根据areaCode查询监控站点列表
     *
     * @param areaCode
     * @return
     */
    public List<String> findMonitorSiteByAreeCode(String areaCode){
        List<String> list = new ArrayList<>();
        try {
            List<SysMonitorSiteInfo> list1= repository.findSysMonitorSiteInfoByAreaCode(areaCode);
            if(list1 !=null){
                for (SysMonitorSiteInfo s:list1){
                    list.add(s.getMonitorSiteName());
                }
            }

        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
        }
        return list;
    }

    /**
     * 修改
     * @param sysMonitorSiteInfo
     * @return
     */
    public Boolean update( SysMonitorSiteInfo sysMonitorSiteInfo) {
        List<SysMonitorSiteInfo> infos =repository.findAllByMonitorSiteNameAndIdNot(sysMonitorSiteInfo.getMonitorSiteName(),sysMonitorSiteInfo.getId());
        if(infos.isEmpty()){
            repository.save(sysMonitorSiteInfo);
            return true;
        }
        return false;
    }

    public Page<SysMonitorSiteInfo> find(int pageIndex, int pageSize, Map<String, Object> params) {

        Page<SysMonitorSiteInfo> industryPage = repository.findAll(new SysMonitorSiteInfoService.SysMonitorSiteInfoSpec(params),
                PageRequest.of(pageIndex, pageSize, Sort.Direction.ASC, "id"));
        return industryPage;
    }


    private class SysMonitorSiteInfoSpec implements Specification<SysMonitorSiteInfo> {
        private Map<String, Object> params = new HashMap<String, Object>();

        public SysMonitorSiteInfoSpec(Map<String, Object> params) {
            this.params = params;
        }

        @Override
        public Predicate toPredicate(Root<SysMonitorSiteInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            List<Predicate> lstPredicates = new ArrayList<Predicate>();

            Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
            return cb.and(lstPredicates.toArray(arrayPredicates));
        }
    }
}
